home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / CLESSONS.ZIP / LESSON7 < prev    next >
Text File  |  1986-02-07  |  17KB  |  342 lines

  1. .NT
  2.  A NOTE ABOUT THE LESSONS in C 
  3. .b4-24R5C4
  4. These were written while the author was ~Ilearning~N  the language and since
  5. .R6C4
  6. they  are  ~Ifree~N ( to  copy  and/or  distribute ) there  is  a money-back
  7. .R7C4
  8. guarantee on the accuracy of each and every statement in the lessons (!)
  9. .R9C4
  10. The  ~Idisplay~N  program was written ( in C ) in order to provide a vehicle
  11. .R10C4
  12. for displaying the lessons.
  13. .R12C5
  14. .B
  15. P.J.Ponzo
  16. .B
  17. Dept. of Applied Math
  18. .B
  19. Univ. of Waterloo
  20. .B
  21. Ontario N2L 3G1
  22. .K16,32
  23. PonzoTUTOR
  24. .WNT
  25.    More FUNCTIONS, including main()   
  26.  
  27.  
  28.  
  29.  
  30.     You may recall, in a previous lesson, that we passed ~b~Ifloat~N arguments
  31.     ~b~Ix~N and ~b~Iy~N to a function ( which we called ~b~Iaverage(x,y)~N ) which
  32.     returned the average: (x+y)/2. It was something like:
  33. .WN
  34. 1  ~b~Imain() {                                                      ~N
  35. 2  ~b~I    float x,y,a;                                              ~N
  36. 3  ~b~I    printf("\n Enter two numbers (separated by a space) : "); ~N
  37. 4  ~b~I    scanf("%f%f",&x,&y);                                      ~N
  38. 5  ~b~I    a=average(x,y);                                           ~N
  39. 6  ~b~I    printf("\n The average of %f and %f is %f",x,y,a);        ~N
  40. 7  ~b~I}                                                             ~N
  41. 8  ~b~Ifloat average (a,b);                                          ~N
  42. 9  ~b~Ifloat a, b;                                                   ~N
  43. 10 ~b~I{                                                             ~N
  44. 11 ~b~I    float z;                                                  ~N
  45. 12 ~b~I    z=(a+b)/2;                                                ~N
  46. 13 ~b~I    return(z);                                                ~N
  47. 14 ~b~I}                                                             ~N
  48.     The function ~b~Iaverage(a,b)~N was invoked in line 5, where we passed
  49.     it ~Icopies~N of the variables ~b~Ix~N and ~b~Iy~N. The function itself
  50.     starts in line 8 and it makes no difference what name the function gives
  51.     to its arguments ..they are, after all, only private copies of the two
  52.     variables which ~b~Imain()~N uses. Whatever ~b~Iaverage~N does with its
  53.     copies, it does NOT effect the ~Ioriginal~N ~b~Ix~N and ~b~Iy~N.
  54. .WR15C1
  55.                                                                               
  56.                                                                               
  57.                                                                               
  58.                                                                               
  59.                                                                               
  60.                                                                               
  61. .R15C1
  62.     We could have simplified the function by replacing lines 11, 12, 13 by:
  63.     ~b~I    return( (a+b)/2 );                                        ~N
  64.  
  65.     Anyway, we mentioned that ~b~Iprintf()~N, ~b~Iscanf()~N, ~b~Igetchar()~N
  66.     and ~b~Imain()~N were ALL functions. Whereas WE write the functions ~b~Imain()~N
  67.     and (in this example) ~b~Iaverage()~N, the others are a ~Istandard~N part
  68.     of the ~IC-library~N.
  69. .WN
  70.  
  71.  
  72.  
  73.     Some C-compilers automatically give your ~b~Imain()~N program access to
  74.     the ~Ist~Nan~Id~Nard ~Ii~Nnput ~Io~Nutput~N ( ~Istdio~N ) routines such
  75.     as ~b~Iprintf()~N, etc. BUT, SOME DO NOT.
  76.  
  77.     To be sure that you include these ~Istdio~N functions in your ~b~Imain()~N
  78.     program (assuming you will be using them) you should begin your program...
  79.  
  80. ~b~I#include <stdio.h>~N     ~IDON'T FORGET TO BEGIN WITH THIS !!~N
  81. ~b~Imain()  {         ~N
  82. ~b~I    ........      ~N
  83. ~b~I    ........      ~N
  84. ~b~I}                 ~N
  85.     When the C-compiler sees the ~b~I#include <stdio.h>~N it will append to
  86.     your ~b~Imain()~N program all the ~Iio~N functions in the C library.
  87. .WN
  88.  
  89.  
  90.  
  91. .T
  92.     don't forget STDIO.H    
  93. .WN
  94.     Now, if ~b~Iprintf()~N and ~b~Iscanf()~N and ~b~Iaverage()~N etc. can be
  95.     passed arguments, why not ~b~Imain()~N ???
  96.  
  97.     As a matter of fact, we CAN pass arguments to ~b~Imain()~N.
  98.  
  99.     Suppose your ~b~Imain()~N program was expected to add a series of numbers
  100.     and that you compiled/linked it with the name ~Isum~N. Now you could run
  101.     the ~Isum~N program by typing its name, ~Isum~N, after which a ~b~Iscanf()~N 
  102.     function call (in the program) would input the numbers which were to be
  103.     added. Then your program would ~b~Iprintf()~N the sum.
  104.  
  105.     BUT, wouldn't it be nice to run the ~Isum~N program by typing:
  106.  
  107. ~Isum  10.5 -15.23 6.7~N   and have the ~Isum~N program run, and also have the
  108.                        numbers ~I10.5 -15.23~N and ~I6.7~N passed to the program
  109.                        as arguments ?
  110. .K19,32
  111. main(?,?)
  112.     We'll talk about how to do this in C....
  113. .WK19,60
  114. wunderbar!
  115. .WNT
  116.     The main() arguments   
  117. .R4C1
  118. ~V0  #include <stdio.h>                                            ~N
  119. 1  ~b~Imain(number,term)                                             ~N
  120. 2  ~b~Iint number;                     /* first  main() argument */  ~N
  121. 3  ~b~Ichar *term[];                   /* second main() argument */  ~N
  122. 4  ~b~I{                               /* NOW start main()       */  ~N
  123. 5  ~b~I  ...........                                                 ~N
  124. 6  ~b~I  ...........                   /* program goes here      */  ~N
  125. 7  ~b~I}                               /* end of main()          */  ~N
  126. .R15C1
  127.      Here we ~V#include~N the stdio.h library (we'll need this stuff).
  128. .WR4C1
  129. 0  ~b~I#include <stdio.h>                                            ~N
  130. ~V1  main(number,term)                                             ~N
  131. .R15C1
  132.                                                                             
  133. .R15C1
  134.     NOW our main program expects two arguments called ~b~Inumber~N and ~b~Iterm~N.
  135. .WR5C1
  136. 1  ~b~Imain(number,term)                                             ~N
  137. ~V2  int number;                     /* first  main() argument */  ~N
  138. ~V3  char *term[];                   /* second main() argument */  ~N
  139. .R15C1
  140.                                                                             
  141. .R15C1
  142.     The first argument (here called ~b~Inumber~N) is ALWAYS an ~b~Iint~Neger!
  143.     The second argument (here called ~b~Iterm~N) is ALWAYS declared as:
  144.     ~b~Ichar *name[]~N;   
  145.           
  146.           name of second argument goes in here (in our example, it's ~b~Iterm~N).
  147. .WR6C1
  148. 2  ~b~Iint number;                     /* first  main() argument */  ~N
  149. .R15C1
  150.                                                                             
  151.                                                                             
  152.                                                                             
  153.                                                                             
  154.                                                                             
  155. .R15C1
  156.     BECAUSE it is declared ~b~Ichar *term~N we see that ~b~Iterm~N is a ~r~Ipointer~N!
  157.     BECAUSE we refer to it as ~b~Iterm[]~N we see that ~b~Iterm~N is an ~IARRAY~N!
  158.  
  159.     In fact, the second argument is ALWAYS an ARRAY of POINTERS which point
  160.     to ~b~Ichar~Nacter strings (that's why we said ~b~Ichar~N).
  161. .WR15C1
  162.                                                                             
  163.                                                                             
  164.                                                                             
  165.                                                                             
  166.                                                                             
  167. .R15C1
  168.     If we called upon our (compiled/linked) program with the command string:
  169.     ~Isum  10.5 -15.23 6.7~N then this contains ~I4~N elements, namely:
  170.     ~Vsum~N and ~V10.5~N and ~V-15.23~N and ~V6.7~N (separated by a space).
  171.     It is the number ~I4~N which gets passed to ~b~Imain()~N as its first
  172.     argument! ( ..in our example, ~b~Inumber~N is the ~b~Iint~Neger ~I4~N).
  173. .WR15C1
  174.                                                                             
  175.                                                                             
  176.                                                                             
  177.                                                                             
  178.                                                                             
  179. .R15C1
  180.     And the second argument?
  181.     The second argument which gets passed to ~b~Imain()~N is (are?) the 
  182.     strings: ~Vsum~N and ~V10.5~N and ~V-15.23~N and ~V6.7~N
  183.     BUT, ~b~Imain()~N receives this list of strings as an ARRAY of POINTERS.
  184.  
  185.     term[0] points to the string ~Vsum~N 
  186.     term[1] points to the string ~V10.5~N 
  187.     term[2] points to the string ~V-15.23~N 
  188.     term[3] points to the string ~V6.7~N 
  189. .WR15C1
  190.                                                                             
  191.                                                                             
  192.                                                                             
  193.                                                                             
  194.                                                                             
  195.                                                                             
  196.                                                                             
  197.                                                                             
  198.                                                                             
  199. .R15C1
  200.     NOTE: when we refer to the ~Istring -15.23~N we mean a collection of 7
  201.           characters: '-' and '1' and '5' and '.' and '2' and '3' and '\0'.
  202.           (remember the '\0' which terminates strings?)
  203.     We may pick out the numbers ~I10.5~N and ~I-15.23~N etc. by referring to
  204.     ~b~Iterm[1]~N and ~b~Iterm[2]~N etc.
  205.  
  206.     Of course, they are NOT (really) numbers (?!@#$) but strings of ASCII
  207.     characters. To add them up, we must convert them to ~b~Ifloat~Ning point
  208.     numbers....
  209. .WK6,32
  210. GO!GO!GO!
  211. .WN
  212.  
  213.  
  214.  
  215.  
  216. .T
  217.     Some SUM programming ...  
  218. .WN
  219. 0  ~b~I#include <stdio.h>                                                ~N
  220. 1  ~b~Imain(number,term)                                                 ~N
  221. 2  ~b~Iint number;                         /* first  main() argument */  ~N
  222. 3  ~b~Ichar *term[];                       /* second main() argument */  ~N
  223. 4  ~b~I{                                   /* NOW start main()       */  ~N
  224. 5  ~b~I    float next, sum=0;              /* couple of floats       */  ~N
  225. 6  ~b~I    int n;  .                       /* an int to count terms  */  ~N
  226. 7  ~b~I    for (n=1; n<number; n++); {     /* loop thru' terms       */  ~N
  227. 8  ~b~I        next=atof(term[n]);         /* CONVERT next term      */  ~N
  228. 9  ~b~I        sum=sum+next;               /* add it to sum          */  ~N
  229. 10 ~b~I        printf("\n    %8.3f",next); /* print each term        */  ~N
  230. 11 ~b~I    }                               /* end of for-loop        */  ~N
  231. 12 ~b~I    printf("\nSUM=%8.3f,sum);       /* print the sum          */  ~N
  232. 13 ~b~I}                                   /* end of main()          */  ~N
  233. .R8C1
  234. ~V7      for (n=1; n<number; n++); {     /* loop thru' terms       */  ~N
  235. .R15C1
  236.     Here we begin our loop, which sums the terms.
  237.     Beginning with ~b~Iterm[1]~N (the first number) we let ~b~In~N cycle thru'
  238.     the terms (incrementing each time, with ~b~In++~N).
  239. .WR8C1
  240. 7  ~b~I    for (n=1; n<number; n++); {     /* loop thru' terms       */  ~N
  241. ~V8          next=atof(term[n]);         /* CONVERT next term      */  ~N
  242. .R15C1
  243.                                                                              
  244.                                                                              
  245.                                                                              
  246. .R15C1
  247.     Here's our conversion of the ASCII string (one for each number) to a
  248.     ~b~Ifloat~N. (This ~Ia~Nscii ~Ito~N ~If~Nloat function is part of any
  249.     respectable C-library ...which is one reason why we #included stdio.h).
  250.     Note that we give to ~b~Iatof()~N the ~r~Ipointer~N to the ASCII string,
  251.     namely ~b~Iterm[n]~N (for each value of ~b~In~N in our for-loop).
  252. .WR9C1
  253. 8  ~b~I        next=atof(term[n]);         /* CONVERT next term      */  ~N
  254. ~V9          sum=sum+next;               /* add it to sum          */  ~N
  255. .R15C1
  256.                                                                              
  257.                                                                              
  258.                                                                              
  259.                                                                              
  260.                                                                              
  261. .R15C1
  262.     Here we add the ~b~Ifloat~N ( generated in Line 8 by ~b~Iatof()~N ) to our
  263.     ~b~Isum~N ( which was initialized to ~b~I0~N in Line 5 ).
  264. .WR10C1
  265. 9  ~b~I        sum=sum+next;               /* add it to sum          */  ~N
  266. ~V10         printf("\n    %8.3f",next); /* print each term        */  ~N
  267. .R15C1
  268.                                                                              
  269.                                                                              
  270.                                                                              
  271. .R15C1
  272.     Just to prove that ~b~Iatof()~N is doing its job, we'll print each ~b~Ifloat~N
  273.     which it generates ...each on a ~b~I\n~Newline.
  274. .WR11C1
  275. 10 ~b~I        printf("\n    %8.3f",next); /* print each term        */  ~N
  276. ~V11     }                               /* end of for-loop        */  ~N
  277. ~V12     printf("\nSUM=%8.3f,sum);       /* print the sum          */  ~N
  278. .R15C1
  279.                                                                              
  280.                                                                              
  281. .R15C1
  282.     We end the for-loop, after which we ~b~Iprintf()~N our ~b~Isum~N (using ~I8~N
  283.     positions on the screen, and printing to ~I3~N decimal places).
  284. .WR12C1
  285. 11 ~b~I    }                               /* end of for-loop        */  ~N
  286. 12 ~b~I    printf("\nSUM=%8.3f,sum);       /* print the sum          */  ~N
  287. ~V13 }                                   /* end of main()          */  ~N
  288. .R15C1
  289.                                                                              
  290.                                                                              
  291. .R15C1
  292.     Finally, we come to the end of ~b~Imain()~N.
  293. .K19,32
  294. magnifique
  295. .WN
  296. 0  ~b~I#include <stdio.h>                                                ~N
  297. 1  ~b~Imain(number,term)                                                 ~N
  298. 2  ~b~Iint number;                         /* first  main() argument */  ~N
  299. 3  ~b~Ichar *term[];                       /* second main() argument */  ~N
  300. 4  ~b~I{                                   /* NOW start main()       */  ~N
  301. 5  ~b~I    float next, sum=0;              /* couple of floats       */  ~N
  302. 6  ~b~I    int n;  .                       /* an int to count terms  */  ~N
  303. 7  ~b~I    for (n=1; n<number; n++); {     /* loop thru' terms       */  ~N
  304. 8  ~b~I        next=atof(term[n]);         /* CONVERT next term      */  ~N
  305. 9  ~b~I        sum=sum+next;               /* add it to sum          */  ~N
  306. 10 ~b~I        printf("\n    %8.3f",next); /* print each term        */  ~N
  307. 11 ~b~I    }                               /* end of for-loop        */  ~N
  308. 12 ~b~I    printf("\nSUM=%8.3f,sum);       /* print the sum          */  ~N
  309. 13 ~b~I}                                   /* end of main()          */  ~N
  310.     In response to ~Isum  10.5 -15.23 6.7~N, our program will print:
  311. ~r~I      10.500~N      Note: Each term is printed after 4 spaces, then 
  312. ~r~I     -15.230~N            occupies a field width of ~I8~N and is
  313. ~r~I       6.700~N            right-justified!
  314. ~r~ISUM=   1.970~N      Note: 4 spaces was just enough for:~r~ISUM=~N.
  315. .K19,62
  316. looks nice
  317. .WN
  318. .R10C1
  319.    Although ( in our program ) ~b~Iterm[0]~N is supposed to point to the ~Iname~N
  320.    of the program ( in our example it was ~Isum~N ) some C-compilers do NOT
  321.    provide this! In particular, MS-DOS does NOT  pass the ~Iname~N to the compiled
  322.    program, so C-compilers running under MS-DOS do not (CANNOT) implement this
  323.    feature of the C language. Instead, reference to this ~Izero~Nth string
  324.    may yield garbage or a blank (NULL) string or some home-made string!
  325. .b8-17
  326. .K19,32
  327.  noname?
  328. .WN
  329.  
  330.  
  331.  
  332.  
  333.  
  334. .T
  335.    That's all folks!   
  336. .K16,32
  337. au revoir!
  338.  
  339.  
  340. .q
  341.  
  342.